   using System;
   using System.Collections.Generic;
   using System.IO;
   using System.Linq;
   using System.Text;
   using Avalonia.Controls;
   using Avalonia.Media.Imaging;
   using JiebaNet.Analyser;
   using JiebaNet.Segmenter;
   using JiebaNet.Segmenter.Common;
   using WordCloudSharp;
   using SixLabors.ImageSharp;
   using SixLabors.ImageSharp.Formats.Jpeg;
   using SixLabors.ImageSharp.Processing;
   using System.Drawing;
   using Image = SixLabors.ImageSharp.Image;

   namespace Mvvm.Helpers
   {
       public class WordCloudHelper
       {
           /// <summary>
           /// 从指定文本中抽取关键词的同时得到其权重
           /// </summary>
           /// <param name="text"></param>
           /// <returns></returns>
           public static WordWeightPair[] ExtractTagsWithWeight(string text)
           {
               var extractor = new TfidfExtractor();
               var wordWeight = extractor.ExtractTagsWithWeight(text, 50);
               StringBuilder sbr = new StringBuilder();
               sbr.Append("词语");
               sbr.Append(",");
               sbr.Append("权重");
               sbr.AppendLine(",");
               foreach (var item in wordWeight)
               {
                   sbr.Append(item.Word);
                   sbr.Append(",");
                   sbr.Append(item.Weight);
                   sbr.AppendLine(",");
               }
               string filename = "关键词权重统计.csv";
               File.WriteAllText(filename, sbr.ToString(), Encoding.UTF8);
               Console.WriteLine("关键词提取完成：" + filename);
               return wordWeight.ToArray();
           }

           /// <summary>
           /// 分词并统计词频：默认为精确模式，同时也使用HMM模型
           /// </summary>
           /// <param name="text"></param>
           /// <param name="wordWeightAry"></param>
           /// <returns></returns>
           public static KeyValuePair<string, int>[] Counter(string text, WordWeightPair[] wordWeightAry)
           {
               var segmenter = new JiebaSegmenter();
               var segments = segmenter.Cut(text);
               var freqs = new Counter<string>(segments);
               KeyValuePair<string, int>[] countAry = new KeyValuePair<string, int>[wordWeightAry.Length];
               for (int i = 0; i < wordWeightAry.Length; i++)
               {
                   string key = wordWeightAry[i].Word;
                   countAry[i] = new KeyValuePair<string, int>(key, freqs[key]);
               }
               StringBuilder sbr = new StringBuilder();
               sbr.Append("词语");
               sbr.Append(",");
               sbr.Append("词频");
               sbr.AppendLine(",");
               foreach (var pair in countAry)
               {
                   sbr.Append(pair.Key);
                   sbr.Append(",");
                   sbr.Append(pair.Value);
                   sbr.AppendLine(",");
               }
               string filename = "词频统计结果.csv";
               File.WriteAllText(filename, sbr.ToString(), Encoding.UTF8);
               Console.WriteLine("词频统计完成：" + filename);
               return countAry;
           }

           /// <summary>
           /// 创建词云图
           /// </summary>
           /// <param name="countAry"></param>
           public static void CreateWordCloud(List<KeyValuePair<string, int>> countAry)
           {
               string markPath = "mask.jpg";
               string resultPath = "result.jpg";
               Console.WriteLine("开始生成图片，读取蒙版：" + markPath);

               using var mask = Image.Load(markPath);
               Console.WriteLine("1");

               // 将 ImageSharp 图像转换为 byte 数组
               using var memoryStream = new MemoryStream();
               mask.SaveAsPng(memoryStream);
               memoryStream.Position = 0;
                // 将字符数组转换成image
                using var systemDrawingMask = System.Drawing.Image.FromStream(memoryStream);
               // 使用 ImageSharp 图像
               var wordCloud = new WordCloud(1000, 1000, false, null, -1, 1, systemDrawingMask, false);
               Console.WriteLine("2");
               var result = wordCloud.Draw(countAry.Select(it => it.Key).ToList(), countAry.Select(it => it.Value).ToList());

               result.Save(resultPath);
               Console.WriteLine("图片生成完成，保存图片：" + resultPath);
           }
       }
   }
   